<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" :class="method==='view'?'readonly':''"  :disabled="method==='view'" @keyup.enter.native="doSubmit()"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="设备名称" prop="checkName.id"
                :rules="[
                 ]">
              <el-input v-model="inputForm.checkName.id" placeholder="请填写内容" disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="类别" prop="checkCategory"
                :rules="[
                   {required: true, message:'类别不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.checkCategory" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('basicdata_category')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="计量单位" prop="checkUnit"
                :rules="[
                  {required: true, message:'计量单位不能为空', trigger:'blur'}
                 ]" v-if="isQuantitative">
                <el-select v-model="inputForm.checkUnit" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('basicdata_symbol_measure')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="判断符号" prop="checkJudgeSymbol"
                :rules="[
                  {required: true, message:'判断符号不能为空', trigger:'blur'}
                 ]" v-if="isQuantitative">
                <el-select v-model="inputForm.checkJudgeSymbol" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('basicdata_symbol')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="上限" prop="checkUp"
                :rules="[
                  {required: true, message:'上限不能为空', trigger:'blur'}
                 ]" v-if="isQuantitative">
              <el-input v-model="inputForm.checkUp" placeholder="请填写上限"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="极限" prop="checkLimit"
                :rules="[
                  {required: true, message:'极限不能为空', trigger:'blur'}
                 ]" v-if="isQuantitative">
              <el-input v-model="inputForm.checkLimit" placeholder="请填写极限"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否手动" prop="checkJudge"
                :rules="[
                  {required: true, message:'是否手动不能为空', trigger:'blur'}
                 ]" v-if="isQuantitative">
                <el-select v-model="inputForm.checkJudge" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="点检标准" prop="checkContent"
                :rules="[
                  {required: true, message:'点检标准不能为空', trigger:'blur'}
                 ]" v-if="isQualitative">
          <el-input type="textarea" v-model="inputForm.checkContent" placeholder="请填写定性分析"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="method === 'add'" type="primary" @click="continueDoSubmit()">继续添加</el-button>
      <el-button @click="visible = false">关闭</el-button>
      <el-button v-if="method !== 'view'" type="primary" @click="doSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        oldInputForm: '',
        inputForm: {
          id: '',
          checkName: {
            id: ''
          },
          checkCategory: '',
          checkUnit: '',
          checkJudgeSymbol: '',
          checkUp: '',
          checkLimit: '',
          checkJudge: '',
          checkContent: '',
          remarks: ''
        },
        isQualitative: true,
        isQuantitative: true
      }
    },
    watch: {
      'inputForm.checkCategory': function (newVal, oldVal) {
          if (newVal === '1') {
            this.inputForm.checkUnit = ''
            this.inputForm.checkJudgeSymbol = ''
            this.inputForm.checkUp = ''
            this.inputForm.checkLimit = ''
            this.inputForm.checkJudge = ''
            this.inputForm.checkContent = ''
            this.inputForm.remarks = ''
            this.isQualitative = true
            this.isQuantitative = false
          }
          if (newVal === '2') {
            this.inputForm.checkUnit = ''
            this.inputForm.checkJudgeSymbol = ''
            this.inputForm.checkUp = ''
            this.inputForm.checkLimit = ''
            this.inputForm.checkJudge = ''
            this.inputForm.checkContent = ''
            this.inputForm.remarks = ''
            this.isQualitative = false
            this.isQuantitative = true
          }
      }
    },
    components: {
    },
    methods: {
      init (method, obj, partDevice) {
        this.inputForm.checkCategory = "2"
        this.method = method
        this.inputForm.checkName.id = partDevice
        if (method === 'add') {
          this.isQualitative = false
          this.isQuantitative = true
          this.title = `新建设备点检附表`
        } else if (method === 'edit') {
          this.title = '修改设备点检附表'
        } else if (method === 'view') {
          this.title = '查看设备点检附表'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = ''
          this.oldInputForm = ''
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.oldInputForm = obj
            this.inputForm = JSON.parse(JSON.stringify(obj))
            this.inputForm.checkName.id = partDevice
          }
        })
      },
   // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.visible = false
          }
        })
      },
      continueDoSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.$refs['inputForm'].resetFields()
          }
        })
      }
    }
  }
</script>
